<?php
namespace intelligent\vcard\client\controllers;

use Yii;
use models\ivcard\Vcard;
use services\Service;
use intelligent\vcard\services\VcardService;


/**
 * 名片相关
 */
class VcardController extends BaseController
{
    protected $allowActions = ['list'];

    /**
     *  名片列表
     *
     * @return string
     */
    public function actionList()
    {
        $vcardService = new VcardService();
        $params = Yii::$app->getRequest()->getQueryParams();
        return $vcardService->listByPage($params);
    }


    /**
     * 添加名片
     *
     * @return string
     */
    public function actionAdd()
    {
        $reqeust = Yii::$app->getRequest();
        $params = $reqeust->post();
        if(empty($params)){
            return Service::fail('请求数据不能为空');
        }
        $userId = Yii::$app->getUser()->getId();
        $params['user_id'] = $userId;
        $vcardService = new VcardService();
        return $vcardService->save($params);
    }

    /**
     * 修改名片
     *
     * @return string
     */
    public function actionUpdate($id)
    {
        $reqeust = Yii::$app->getRequest();
        $params = $reqeust->post();
        if(empty($params)){
            return Service::fail('请求数据不能为空');
        }
        $userId = Yii::$app->getUser()->getId();
        $vcardService = new VcardService();
        return $vcardService->save($params, ['id'=>$id, 'user_id'=>$userId]);
    }

    /**
     * 当前名片信息
     *
     * @return string
     */
    public function actionInfo()
    {
        $userId = Yii::$app->getUser()->getId();
    }

    /**
     * 当前名片信息
     *
     * @return string
     */
    public function actionDefaultInfo()
    {
        $vcardService = new VcardService();
        $userId = Yii::$app->getUser()->getId();
        return $vcardService->getDefault($userId);
    }

    /**
     * 我的名片收藏夹
     *
     * @return string
     */
    public function actionMyCollection()
    {
        $userId = Yii::$app->getUser()->getId();
        $vcardService = new VcardService();
        return $vcardService->getMyCollection($userId);
    }

    /**
     * 收藏名片
     *
     * @return string
     */
    public function actionStar()
    {
        $userId = Yii::$app->getUser()->getId();
        $reqeust = Yii::$app->getRequest();
        if(!($toUserId = $reqeust->post('to_user_id'))){
            return $this->fail('被收藏的名片人必传！');
        }
        $fromUserId = $reqeust->post('from_user_id', 0);
        $vcardService = new VcardService();
        return $vcardService->star($userId, $toUserId, $fromUserId);
    }
}
